/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class defines values of the <i>'a option</i> type.
 *
 * @param <T> type of embedded type
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class Option<T extends ToValue> implements ToValue {

    /** The embedded value if any (<tt>null</tt> otherwise). */
    private final T value;

    /**
     * Constructs a <i>None</i> value.
     */
    public Option() {
        this.value = null;
    } // end empty constructor

    /**
     * Constructs a <i>Some</i> value.
     * @param v value to embedd - should not be <tt>null</tt>
     */
    public Option(final T v) {
        assert v != null : "null v";
        this.value = v;
    } // end constructor(T)

    /**
     * Tests whether the instance is a <i>Some</i> value.
     * @return <tt>true</tt> if the instance is a <i>Some</i> value,
     *         <tt>false</tt> otherwise
     */
    public boolean isSome() {
        return this.value != null;
    } // end method 'isSome()'

    /**
     * Returns the embedded value.
     * @return the embedded value
     * @throws NoValue if the value is <i>None</i>
     */
    public T get() throws NoValue {
        if (this.value != null) {
            return this.value;
        } else {
            throw new NoValue();
        } // end if/else
    } // end method 'get()'

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        if (this.value != null) {
            final Block res = Block.createBlock(0, this.value.toValue());
            return Value.createFromBlock(res);
        } else {
            return Value.ZERO;
        } // end if/else
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        if (this.value != null) {
            return this.value.hashCode();
        } else {
            return 0;
        } // end if/else
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof Option) {
            final Option<?> that = (Option<?>) obj;
            return ((this.value == null) && (that.value == null))
                || ((this.value != null) && this.value.equals(that.value));
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        if (this.value != null) {
            return "Some(" + this.value.toString() + ")";
        } else {
            return "None";
        } // end if/else
    } // end method 'toString()'

    /**
     * This class defines the exception to be raised if an attempt is made
     * to retrieve the value from a <i>None</i> value.
     */
    public static final class NoValue extends RuntimeException {

        /** Serialization UID. */
        static final long serialVersionUID = -8037983505083402314L;

        /**
         * Constructs an instance with predefined message.
         */
        public NoValue() {
            super("No value");
        } // end empty constructor

    } // end inner-class 'NoValue'

} // end class 'Option'
